/**
 * @name variable-search
 * @id python/variable-search
 */

import python
import util

// predicate getFunctionScope(File file,Function func) {

//     func.getEnclosingModule().getFile()=file
// }

// from File f,Module mod, Function func,Parameter p,string fileName,string pName,string def
// where 
// fileName=f.getBaseName() and
// mod.getFile()=f and
// func.getEnclosingModule()=mod and
// pName=p.getName() and
// def=func.getName() and
// exists(func.getArgByName(pName))
// select fileName,def,pName








string getVariableInfo(File f,Scope scope,Expr expr)
{
    scope=expr.getScope() and
    scope.getEnclosingModule().getFile()=f and
    result=f.getBaseName()+","+expr.toString()+","+expr.getLocation()
}





string getFullVarName(File f,Module mod,Scope scope,Name expr)
{
    isParentOfExpr(f, mod, scope, expr) and
    result=f.getAbsolutePath()+","+getScopeName(scope)+","+expr.getId()
}


string getFullReferenceVarName(File f,Module mod,Scope scope,Name expr)
{
    isParentOfExpr(f, mod, scope, expr) and
    result=expr.getLocation().toString() + ","+scope.toString() + getFullVarName(f, mod, scope, expr)

}

// string getFunctionRepresent(Function func)
// {
//     forall( |  | )
//     if exists(func.getArgName(i)) then
//         result=result+func.getArgName(i)+getFunctionRepresent(func, i+1)
//     else
//         result=result
// }

string getFullFunctionName(File f,Module mod,Scope scope,FunctionDef func)
{
    isParentOfStmt(f, mod, scope, func) and
    result=f.getAbsolutePath()+","+ getScopeName(scope)+","+func.getDefinedFunction().toString()
}


from File f,Module mod,Scope scope,Variable varText,Stmt ownerStmt,Name name,Location location,string loc,string filePath,string modName,string scopeName,string varName
where 
isSource(f) and
name=varText.getAStore() and
ownerStmt.getASubExpression().defines(varText) and
scope=name.getScope() and
mod=scope.getEnclosingModule() and
f=mod.getFile() and
location=name.getLocation() and
loc=getLocText(location) and
filePath=f.getAbsolutePath() and
modName=mod.toString() and
scopeName=getScopeName(scope) and
varName=name.getId()
select filePath,modName,scopeName,ownerStmt,varText,varName,loc

// from Variable v

// select v


// from File f,Module mod,Scope scope,Stmt stmt,Expr varName,Location loc,string filePath,string moduleName,string scopeName
// where varName=getAllVariable(stmt, varName) and
// loc=varName.getLocation() and
// isParentOfExpr(f, mod, scope, varName) and
// filePath=f.getAbsolutePath() and
// scopeName=scope.toString() and
// moduleName=mod.toString()

// select filePath,moduleName,scopeName,varName,loc




// from File f,Module mod,Scope scope,Stmt stmt,Expr expr,Location loc,string detail,string ref
// where expr=getAllVariable(stmt, expr) and
// ref=getFullReferenceVarName(f, mod, scope, expr) and
// detail= getFullVarName(f, mod, scope, expr) and
// loc=expr.getLocation() and
// isParentOfExpr(f, mod, scope, expr)
// select expr,mod,scope,loc,detail,ref



// from AssignStmt s,Scope scope,StmtList body,Expr e1,Expr e2
// where scope=s.getScope() and
// body=scope.getBody() and
// e2=s.getValue() and
// e1=s.getATarget()
// select e1,e2,scope